package com.hpin.fansea.service;

import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.adapter.SurroundListAdapter;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.SurroundBussniseList;
import com.hpin.fansea.bean.SurroundListBean;
import com.hpin.fansea.pulltorefush.PullToRefreshBase;
import com.hpin.fansea.pulltorefush.PullToRefreshListView;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;

import java.util.ArrayList;
import java.util.List;

/**
 *
 *  周边商家列表页面
 * Created by Aaron on 15/9/23.
 */
public class SurroundListActivity extends BaseActivity {

    private TextView tv_title;
    private PullToRefreshListView plv_surround;

    private int pageIndex = 0;
    private String type;
    private List<SurroundBussniseList> surroundResultList = new ArrayList<SurroundBussniseList>();
    private SurroundListAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_surround_list);

        initWidget();

        type = getIntent().getStringExtra("type");

        tv_title.setText(getIntent().getStringExtra("title"));

        getSurroundData();

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        tv_title = (TextView) findViewById(R.id.tv_title);

        iv_left.setOnClickListener(this);

        plv_surround = (PullToRefreshListView) findViewById(R.id.plv_surround);

        plv_surround.setMode(PullToRefreshBase.Mode.BOTH);

        mAdapter = new SurroundListAdapter(this, surroundResultList);
        plv_surround.setAdapter(mAdapter);

        plv_surround.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                String label = DateUtils.formatDateTime(SurroundListActivity.this, System.currentTimeMillis(),
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

                pageIndex = 0;
                surroundResultList.clear();
                getSurroundData();

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

                String label = DateUtils.formatDateTime(SurroundListActivity.this, System.currentTimeMillis(),
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

                getSurroundData();

            }
        });

    }

    /**
     * 获取周边商家数据
     */
    private void getSurroundData() {

        HttpUtil.getInstance().setGetRequest("获取中...", this, Constant.WEB_ROOT + Constant.NEARBUSSNISELIST
                + "?token=" + sp.getString(Constant.token, "") + "&dictId=" + "" + "&busType=" + type
                + "&communityId=" + sp.getString(Constant.communityId, "") + "&pageSize="
                + Constant.PAGECOUNT + "&pageIndex=" + (++pageIndex)
                + "&systemType=" + Constant.SYSTEM_TYPE, new HttpUtil.SuccessListener() {

            @Override
            public void setOnSuccessListener(String str) {

                try {

                    plv_surround.onRefreshComplete();

                    SurroundListBean listBean = JSONObject.parseObject(str, SurroundListBean.class);

                    if (Constant.STATUS_SUCESS.equals(listBean.status)) {

//                        List<SurroundBussniseList> result = listBean.result;

                        setListView(listBean.result);
                        mAdapter.setData(surroundResultList);
                        mAdapter.notifyDataSetChanged();

                    } else {
                        showToast(listBean.message);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    showToast("获取数据失败");
                }

            }

        }, new HttpUtil.FailListener() {
            @Override
            public void setOnFailListener() {

                plv_surround.onRefreshComplete();

            }
        });

    }

    /**
     * 判断是否有重复并更新
     */
    private void setListView(List<SurroundBussniseList> tempGuideLists) {

        if (tempGuideLists == null || tempGuideLists.size() == 0)
            return;

        if (surroundResultList.size() == 0) {

            surroundResultList.addAll(tempGuideLists);
            return;

        }

        for (SurroundBussniseList goods : tempGuideLists) {
            boolean exist = false;
            for (SurroundBussniseList g : surroundResultList) {
                if (goods.id_.equals(g.id_)) {
                    exist = true;
                    break;
                }
            }
            if (!exist)
                surroundResultList.add(goods);
        }

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:

                finish();

                break;
        }

    }
}